<template>
    <div class="goods-container">

        <!-- 在网页中跳转方式有两种 -->
        <!-- 1： a标签 即标签跳转-->
        <!-- 2： 使用window.location.href的形式 即编程式导航-->

        <!--<router-link class="good-item" v-for="item in listArr" :key="item.id" :to="'/home/GoodsInfo' + item.id" tag="div">-->
            <!--<img :src="item.img">-->
            <!--<h1>{{item.title}}</h1>-->
            <!--<div class="info">-->
                <!--<div class="price">-->
                    <!--<span class="new">￥{{item.sellPrice}}</span>-->
                    <!--<span class="old">￥{{item.marketPrice}}</span>-->
                <!--</div>-->
                <!--<div class="sell">-->
                    <!--<span>热卖中</span>-->
                    <!--<span>剩{{item.residue}}件</span>-->
                <!--</div>-->
            <!--</div>-->
        <!--</router-link>-->


        <div class="good-item" v-for="item in listArr" :key="item.id" @click="entryPage(item.id)">
            <img :src="item.img">
            <h1>{{item.title}}</h1>
            <div class="info">
                <div class="price">
                    <span class="new">￥{{item.sellPrice}}</span>
                    <span class="old">￥{{item.marketPrice}}</span>
                </div>
                <div class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.residue}}件</span>
                </div>
            </div>
        </div>


        <mt-button type="primary" size="large" @click="getMore">加载下一页...</mt-button>

    </div>
</template>

<script>
export default {
    data () {
        return {
            pageNumber: 1,
            listArr: null
        }
    },
    created () {
        this.acquireList();
    },
    methods:{
        acquireList () {
            var st = 'http://rap2api.taobao.org/app/mock/149003/goodlist?number=' + this.pageNumber;
            this.$http.get(st)
                .then(result => {
                    var dic = result.body;
                    if (dic['status'] == 200) {
                        if (this.pageNumber == 1) {
                            this.listArr = dic['list']
                        }  else {
                            this.listArr = this.listArr.concat(dic['list'])
                        }
                    }
                })
        },
        getMore () {
            this.pageNumber += 1;
            this.acquireList();
        },
        entryPage (id) {
            /* 使用JS进行路由导航 */
            /* 区分 $route 和 $router 两个对象 */
            /* $route 【路由参数】对象，所有路由中的参数 params， query 都属于他 */
            /* $router 【路由导航】对象， 可以方便的实现路由的前进、后退、url跳转 */
            this.$router.push('/home/GoodsInfo/' + id)
            // this.$router.push({name: 'GoodInfo', params: {id}});
        }
    }
}
</script>

<style lang="scss" scoped>
    .goods-container {
        display: flex;
        flex-wrap: wrap;
        padding: 7px;
        justify-content: space-between;

        .good-item {
            width: 49%;
            border: 1px solid #ccc;
            box-shadow: 0 0 8px #ccc;
            margin: 4px 0;
            padding: 2px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            img {
                width: 100%;
                height: 180px;
                object-fit: cover;
            }
            h1 {
                font-size: 16px;
                font-weight: normal;
            }

            .info {
                background-color: #ddd;
                .price {
                    .new {
                        color:red;
                        font-size: 16px;
                        font-weight: bold;
                    }
                    .old {
                        color:#bbb;
                        font-size: 14px;
                        text-decoration: line-through;
                    }
                }
                .sell {
                    display: flex;
                    justify-content: space-between;
                    font-size: 14px;
                }
            }
        }
    }
</style>
